package game.engine.base

/**
 *
 * countIdx:表示的是执行的次数索引   0 表示第1次执行完毕.
 */
class FrameFunc(val delay: Float, val count: Int, var duration: Float, var action: (countIdx:Int) -> Unit) {
    var elapseTime = 0f
    var started = false
    var executedCount = 0
    var completed = false

    fun execute(dt: Float) {
        elapseTime += dt
        if (started) {
            if (elapseTime >= duration) {
                action.invoke(executedCount)
                elapseTime = 0f
                checkEnd()
            }
        } else {
            if (elapseTime >= delay) {
                started = true
                action.invoke(executedCount)
                elapseTime = 0f
                checkEnd()
            }
        }
    }

    private fun checkEnd() {
        executedCount++
        if (executedCount >= count) {
            completed = true
        }
    }
}
